<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/3/28
 * Time: 09:53
 */

namespace app\admin\controller;


use app\common\controller\Backend;
use app\common\utils\Constant;
use fast\Tree;
use think\Db;
use think\Exception;

/**
 * 业主管理
 * Class User
 * @package app\admin\controller
 */
class User extends Backend
{
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->where(array('user_type'=>Constant::USER_TYPE_OWNER))
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->where(array('user_type'=>Constant::USER_TYPE_OWNER))
                ->field(['password', 'salt', 'token'], true)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    public function send_list($ids=''){
        if ($this->request->isAjax())
        {
            $this->model = model('RedEnvelope');
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $map['user_id'] = $ids;
            $total = $this->model
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $nickname = model('User')->where(array('id'=>$ids))->value('nickname');
            foreach ($list as $key=>&$value){
                $value['nickname'] = $nickname;
            }
            unset($value);
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        $this->assignconfig('user_id',$ids);
        return $this->view->fetch();
    }
}